import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "@/views/Login";
import Admin from "@/views/admin/Admin";
import Welcome from "@/views/admin/Welcome";
import Member from "@/views/admin/department/Member";
import Purchase from "@/views/admin/operate/Purchase";
import Finance from "@/views/admin/operate/Finance";
import Product from "@/views/admin/operate/Product";
import Department from "@/views/admin/department/Department";
import Hr from "@/views/admin/department/Hr";
import Demand from "@/views/admin/data/Demand";
import Psychology from "@/views/admin/data/Psychology";
import Location from "@/views/admin/data/Location";
import Notice from "@/views/admin/sys/Notice";
import Opinion from "@/views/admin/sys/Opinion";
import ro from "element-ui/src/locale/lang/ro";
import el from "element-ui/src/locale/lang/el";


Vue.use(VueRouter)
//以下代码解决路由地址重复点击的报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

const routes = [
    {
        path: '/',
        name: 'Login',
        component: Login
    },
    {
        path: '/admin',
        name: 'Admin',
        redirect: "/admin/welcome",
        component: Admin,
        children: [
            {
                path: 'welcome',
                component: Welcome
            },
            {
                path: 'purchase',
                component: Purchase
            },

            {
                path: 'product',
                component: Product
            },

            {
                path: 'finance',
                component: Finance
            },
            {
                path: 'department',
                component: Department
            },
            {
                path: 'hr',
                component: Hr
            },
            {
                path: 'member',
                component: Member
            },

            {
                path: 'demand',
                component: Demand
            },

            {
                path: 'psychology',
                component: Psychology
            },
            {
                path: 'location',
                component: Location
            },

            {
                path: 'notice',
                component: Notice
            },

            {
                path: 'opinion',
                component: Opinion
            },
        ]
    },
]

const router = new VueRouter({
    mode: 'hash',
    base: process.env.BASE_URL,
    routes
})
router.beforeEach(((to, from, next) => {
    if (to.path.startsWith("/admin")) {
        let token = sessionStorage.getItem("token");
        if (token == null) {
            next("/");
        }
    }
    next();
}));
export default router
